package com.energy.analysis.basic.controller;

import com.energy.analysis.basic.entity.*;
import com.energy.analysis.basic.service.CollectService;
import com.energy.analysis.basic.service.PriceService;
import com.energy.analysis.basic.service.StandService;
import com.energy.analysis.basic.service.StationService;
import com.energy.analysis.comm.base.BaseController;
import com.energy.analysis.comm.page.Page;
import com.energy.analysis.comm.utils.DateUtils;
import com.energy.analysis.comm.utils.MapAndBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("rawtypes")
@RequestMapping(value = "/basic/stand")
@Controller
public class StandController extends BaseController {


    @Autowired
    private StationService stationService;

    @Autowired
    private StandService standService;

    @Autowired
    private CollectService collectService;

    //换热站列表
    //@RequiresPermissions("basic_station_list:view")
    @RequestMapping("/list")
    @ResponseBody
    public ModelAndView list(HttpServletRequest request, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("station/station");
        List<StationData> stationDataList = stationService.getStationData();
        List<StandData> standDataList = standService.getStandData();
        modelAndView.addObject("stationDataList", stationDataList);
        modelAndView.addObject("standDataList", standDataList);
        return modelAndView;
    }

    //换热站页面上table编辑
    @RequestMapping("/updateInfoWithTable")
    @ResponseBody
    public boolean updatePrice(HttpServletRequest request, @RequestParam boolean stationFlag,
                               @RequestParam int inOrder, @RequestParam int tId, @RequestParam String tValue) {
        try {
            if (stationFlag == true) {//如果修改的是换热站信息
                StationData stationData = stationService.getStationDataById(tId);
                switch (inOrder) {
                    case 1:
                        stationData.setPersonnel(tValue);//如果修改的是负责人
                        break;
                    case 2:
                        stationData.setTelephone(tValue);
                    case 3:
                        stationData.setAddress(tValue);
                    case 4:
                        stationData.setCoordinate(tValue);
                    default:
                        break;
                }
                return stationService.updateStationData(stationData);
            } else {
                StandData standData = standService.getStandDataById(tId);
                if (inOrder == 1) {
                    standData.setDesignArea(Integer.parseInt(tValue == null? "0" : tValue));
                } else {
                    standData.setRealArea(Integer.parseInt(tValue == null? "0" : tValue));
                }
                return standService.updateStandData(standData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    //换热站相关信息/实时数据
    //@RequiresPermissions("station:actualdata")
    @RequestMapping("/actualdata")
    public ModelAndView actualdata(HttpServletRequest request, HttpSession session) {
        pageInitToSession(session);
        ModelAndView modelAndView = new ModelAndView("station/actualdata");
        List<CollectExtendData> collectExtendDataList = new ArrayList<CollectExtendData>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("eTime", DateUtils.getTodayNow());
        map.put("groupType", 2);//接机组分组汇总
        addCollectExtendActualData(collectExtendDataList, map);
        modelAndView.addObject("collectExtendDataList", collectExtendDataList);
        return modelAndView;
    }

    //换热站相关信息/历史数据
    //@RequiresPermissions("station:historydata")
    @RequestMapping("/historydata")
    public ModelAndView historydata(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("station/historydata");
        return modelAndView;
    }

    @SuppressWarnings("unchecked")
    //@RequiresPermissions("station:actualdata")
    @RequestMapping("/actualdataAjax")
    @ResponseBody
    public Page<CollectData> actualdataAjax(HttpServletRequest request, HttpSession session) {
        pageInitToSession(session);
        Page page = new Page();
        List<CollectExtendData> collectExtendDataList = new ArrayList<CollectExtendData>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("eTime", DateUtils.getTodayNow());
        map.put("groupType", 2);//接机组分组汇总
        addCollectExtendActualData(collectExtendDataList, map);
        page.setData(collectExtendDataList);
        return page;
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("/actualdataAjax/export")
    public void actualdataExport(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        List<CollectExtendData> collectExtendDataList = new ArrayList<CollectExtendData>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("eTime", DateUtils.getTodayNow());
        map.put("groupType", 2);//接机组分组汇总
        addCollectExtendActualData(collectExtendDataList, map);
        List<Map<String, Object>> retList = MapAndBeanUtil.objectsToMaps(collectExtendDataList);
        String fileName = "瞬时数据" + DateUtils.getYmdHmsDate() + ".xls";
        exportFile(response, retList, "actualdata", fileName, null);
    }

    @SuppressWarnings("unchecked")
    //@RequiresPermissions("station:actualdata")
    @RequestMapping("/historydataAjax")
    @ResponseBody
    public Page<CollectData> historydata(HttpServletRequest request, HttpSession session) {
        pageInitToSession(session);
        Page page = new Page();
        List<CollectExtendData> collectExtendDataList = new ArrayList<CollectExtendData>();
        Map<String, Object> map = new HashMap<String, Object>();
        String tTime = request.getParameter("tTime") + ":00";
        map.put("eTime", tTime);
        map.put("groupType", 2);//接机组分组汇总
        addCollectExtendActualData(collectExtendDataList, map);
        page.setData(collectExtendDataList);
        return page;
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("/historydataAjax/export")
    public void historydataExport(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
        Page page = new Page();
        List<CollectExtendData> collectExtendDataList = new ArrayList<CollectExtendData>();
        Map<String, Object> map = new HashMap<String, Object>();
        String tTime = request.getParameter("tTime") + ":00";
        map.put("eTime", tTime);
        map.put("groupType", 2);//按机组分组汇总
        addCollectExtendActualData(collectExtendDataList, map);
        List<Map<String, Object>> retList = MapAndBeanUtil.objectsToMaps(collectExtendDataList);
        String fileName = "历史数据" + DateUtils.getYmdHmsDate() + ".xls";
        exportFile(response, retList, "actualdata", fileName, tTime);
    }

    //换热站相关信息/历史曲线
    //@RequiresPermissions("station:historycurve")
    @RequestMapping("/historycurve")
    public ModelAndView historycurve(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("station/historycurve");
        List<StationData> stationDataList = stationService.getStationData();
        List<StandData> standDataList = standService.getStandData();
        modelAndView.addObject("stationDataList", stationDataList);
        modelAndView.addObject("standDataList", standDataList);
        return modelAndView;
    }

    //换热站相关信息/历史曲线
    @SuppressWarnings("unchecked")
    //@RequiresPermissions("station:actualdata")
    @RequestMapping("/historycurveAjax")
    @ResponseBody
    public List<CollectExtendData> historycurveAjax(HttpServletRequest request, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        String standId = request.getParameter("standSelect");
        String bTime = request.getParameter("tTime") + ":00";
        int timeRange = Integer.parseInt(request.getParameter("timeRange"));//时间范围
        String timeType = request.getParameter("timeType");
        String eTime = DateUtils.getTodayNow();
        if ("minute".equals(timeType)) {
            eTime = DateUtils.fixMinute(bTime, timeRange);
        }
        if ("hour".equals(timeType)) {
            eTime = DateUtils.fixHour(bTime, timeRange);
        }
        if ("day".equals(timeType)) {
            eTime = DateUtils.fixDay(bTime, timeRange);
        }
        map.put("bTime", bTime);
        map.put("eTime", eTime);
        map.put("standId", standId);
        List<CollectData> collectDataList = collectService.getCollectDateByStandId(map);
        List<CollectExtendData> collectExtendDataList = new ArrayList<CollectExtendData>();
        for (CollectData collectData : collectDataList) {
            CollectExtendData collectExtendData = new CollectExtendData();
            collectExtendData.setPt3_pt4(collectData.getPt3() - collectData.getPt4());//压差
            collectExtendData.setOpTimeStr(new SimpleDateFormat("yy-MM-dd HH:mm").format(collectData.getOpTime()));
            collectExtendData.setPt1(collectData.getPt1());
            collectExtendData.setPt2(collectData.getPt2());
            collectExtendData.setTe1(collectData.getTe1());
            collectExtendData.setTe2(collectData.getTe2());
            collectExtendData.setTe1_te2(collectData.getTe1() - collectData.getTe2());
            collectExtendData.setCvi1(collectData.getCvi1());
            collectExtendData.setQi(collectData.getQi());
            collectExtendData.setFt1(collectData.getFt1());
            collectExtendData.setFt1q(collectData.getFt1q());
            collectExtendData.setPt3(collectData.getPt3());
            collectExtendData.setPt4(collectData.getPt4());
            collectExtendData.setTe3(collectData.getTe3());
            collectExtendData.setTe4(collectData.getTe4());
            collectExtendData.setTe3_te4(collectData.getTe3() - collectData.getTe4());
            collectExtendData.setFt2(collectData.getFt2());
            collectExtendData.setFt3(collectData.getFt3());
            collectExtendData.setFc1v1(collectData.getFc1v1());
            collectExtendData.setLt1(collectData.getLt1());
            collectExtendData.setQqi(collectData.getQqi());
            collectExtendData.setJqi(collectData.getJqi());
            collectExtendData.setFt3q(collectData.getFt3q());
            collectExtendData.setFt2q(collectData.getFt2q());
            collectExtendData.setFt3qPrice(collectData.getFt3qPrice());
            collectExtendData.setJqiPrice(collectData.getJqiPrice());
            collectExtendData.setQqiPrice(collectData.getQqiPrice());
            collectExtendDataList.add(collectExtendData);
        }
        return collectExtendDataList;
    }

    //换热站相关信息/一网排序
    //@RequiresPermissions("station:onenetwork")
    @RequestMapping("/onenetwork")
    public ModelAndView onenetwork(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("station/onenetwork");
        return modelAndView;
    }

    //换热站相关信息/一网排序
    //@RequiresPermissions("station:twonetwork")
    @RequestMapping("/twonetwork")
    public ModelAndView twonetwork(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("station/twonetwork");
        return modelAndView;
    }

    @SuppressWarnings("unchecked")
    //@RequiresPermissions("station:actualdata")
    @RequestMapping("/netWorkAjax")
    @ResponseBody
    public Page<CollectData> oneNetWorkAjax(HttpServletRequest request, HttpSession session) {
        Page page = new Page();
        List<CollectExtendData> collectExtendDataList = new ArrayList<CollectExtendData>();
        Map<String, Object> map = new HashMap<String, Object>();
        String tTime = request.getParameter("tTime") + ":00";
        map.put("eTime", tTime);
        int curveSelect = Integer.parseInt(request.getParameter("curveSelect"));
        if (curveSelect == 1 || curveSelect == 2) {//3或4是二网排序
            map.put("groupType", 1);//接换热站分组汇总
        } else {
            map.put("groupType", 2);//接换热站分组汇总
        }
        addCollectExtendActualData(collectExtendDataList, map);
        page.setData(collectExtendDataList);
        return page;
    }

    //组装数据用的
    public List<CollectExtendData> addCollectExtendActualData(List<CollectExtendData> collectExtendDataList, Map map) {

        //取一下机组信息
        List<StandData> standDataList = standService.getStandData();
        List<CollectData> collectDataList = collectService.getCollectDate(map);
        //取一下换热站信息
        List<StationData> stationDataList = stationService.getStationData();
        for (CollectData collectData : collectDataList) {
            CollectExtendData collectExtendData = new CollectExtendData();
            for (StandData standData : standDataList) {
            	if (collectData.getStandId().equals(standData.getStandId())) {
                    collectExtendData.setStandName(standData.getStandName());
                }
            }
            for (StationData stationData : stationDataList) {
            	if (collectData.getStationId().equals(stationData.getStationId())) {
                    collectExtendData.setStationName(stationData.getStationName());
                }
            }
            collectExtendData.setOpTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(collectData.getOpTime()));
            collectExtendData.setPt1(collectData.getPt1());
            collectExtendData.setPt2(collectData.getPt2());
            collectExtendData.setPt1_pt2(collectData.getPt1() - collectData.getPt2());//一网压差
            collectExtendData.setTe1(collectData.getTe1());
            collectExtendData.setTe2(collectData.getTe2());
            collectExtendData.setTe1_te2(collectData.getTe1() - collectData.getTe2());//一网温差
            collectExtendData.setCvi1(collectData.getCvi1());
            collectExtendData.setQi(collectData.getQi());
            collectExtendData.setFt1(collectData.getFt1());
            collectExtendData.setFt1q(collectData.getFt1q());
            collectExtendData.setPt3(collectData.getPt3());
            collectExtendData.setPt4(collectData.getPt4());
            collectExtendData.setPt3_pt4(collectData.getPt3() - collectData.getPt4());//二网压差
            collectExtendData.setTe3(collectData.getTe3());
            collectExtendData.setTe4(collectData.getTe4());
            collectExtendData.setTe3_te4(collectData.getTe3() - collectData.getTe4());
            collectExtendData.setFt2(collectData.getFt2());
            collectExtendData.setFt3(collectData.getFt3());
            collectExtendData.setFc1v1(collectData.getFc1v1());
            collectExtendData.setLt1(collectData.getLt1());
            collectExtendData.setQqi(collectData.getQqi());
            collectExtendData.setJqi(collectData.getJqi());
            collectExtendData.setFt3q(collectData.getFt3q());
            collectExtendData.setFt2q(collectData.getFt2q());
            collectExtendData.setFt3qPrice(collectData.getFt3qPrice());
            collectExtendData.setJqiPrice(collectData.getJqiPrice());
            collectExtendData.setQqiPrice(collectData.getQqiPrice());
            collectExtendDataList.add(collectExtendData);
        }
        return collectExtendDataList;
    }

    /**
     * 换热站信息全网分布使用
     */
    @RequestMapping("/all/network")
    public ModelAndView allNetwork(HttpServletRequest request, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("station/all_network");
        pageInitToSession(session);
        return modelAndView;
    }

    /**
     * 换热站信息全网分布中-获取站的实时信息
     */
    @RequestMapping("/all/network/rtNw")
    @ResponseBody
    public List<Map<String, Object>> allNetworkRtNwData(HttpServletRequest request, HttpSession session) {
        List<Map<String, Object>> retList = new ArrayList<Map<String, Object>>();
        //最大批次号
        Integer maxBatchNumber = collectService.getMaxBatchNumber();
        if (null == maxBatchNumber || 0 == maxBatchNumber) {
            return retList;
        }
        return collectService.getStationRtNwData(maxBatchNumber);
    }

}